package naga.x.util;

import java.lang.reflect.Method;

public class EnumUtils {
	
	public static <E extends Enum<E>> E forOrdinal(int ordinal, Class<E> enumClass) {
		E e = null;
		try {
			// 先检测是否有静态方法: E forCode(int)
			Method m = enumClass.getDeclaredMethod("forCode", int.class);
			m.setAccessible(true);
			e = (E) m.invoke(null, ordinal);
		} catch (Exception ex) {}
		if (e == null) {
			E[] vals = enumClass.getEnumConstants();
			e = ordinal >= 0 && ordinal < vals.length ? vals[ordinal] : null;
		}
		return e;
	}
	
}
